package com.hele.sts.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.List;

/**
 * @ClassName AdminUserPermDao
 * @Description TODO
 * @Author xuning
 * @Date 2019-05-14 11:12
 **/
@Repository
public class AdminUserPermDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdminUserPermDao.class);

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public List<XtRole> getRoleList(String userId)
    {
        final String sql = "select a.* from xt_role a,xt_user_role b where a.id = b.role_id and b.user_id = ?";
        RowMapper<XtRole> rowMapper = new BeanPropertyRowMapper<>(XtRole.class);
        return this.jdbcTemplate.query(sql,new Object[]{userId},rowMapper);
    }

    public List<XtMenu> getMenuList(String userId) {
        final String sql = "select a.* from xt_menu a,xt_user_role b,xt_role_menu c where a.id = c.menu_id and c.role_id=b.role_id and b.user_id = ? and status=1 order by levels, num ";
        RowMapper<XtMenu> rowMapper = new BeanPropertyRowMapper<>(XtMenu.class);
        return this.jdbcTemplate.query(sql,new Object[]{userId}, rowMapper);
    }
}
